import { message } from 'antd';
import  axios,{AxiosInstance,InternalAxiosRequestConfig,AxiosResponse} from 'axios'
import { store } from '@/store'

const http:AxiosInstance = axios.create({
    baseURL: "https://www.demo.com",
    // baseURL: process.env.REACT_APP_API_URL,
    timeout: 5000
})

// 请求拦截器
http.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
    const {token} = store.getState().authSlice
    if(token){
        //Bearer是一种认证类型 表示的是后面跟着的是个令牌
        config.headers['Authorization']=`Bearer ${token}`
    }
    return config
    
})



// 响应拦截器
http.interceptors.response.use((response:AxiosResponse)=>{
    const res = response.data
    if(res.code !== 200){
        message.error(res.code+':'+res.message)
        return Promise.reject(new Error(res.message))
    }
    return res
})

export default http
